/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-08-03
 * Time: 23:45
 */
class Solution3 {
    public String convert(String s, int numRows) {
        StringBuilder str = new StringBuilder();
        int d = 2*numRows-2,n = s.length();
        if(numRows == 1) {
            return s;
        }

        //1.处理第一行
        for(int i = 0; i < n; i += d) {
            str.append(s.charAt(i));
        }
        // 处理中间
        for(int k = 1; k < numRows-1; k++) {
            for(int i = k, j = d - i; i < n; i+= d, j += d) {
                if(i < n) {
                    str.append(s.charAt(i));
                }
                if(j < n) {
                    str.append(s.charAt(j));
                }
            }
        }
        //3.处理最后一行
        for(int i = numRows-1; i < n; i += d) {
            str.append(s.charAt(i));
        }
        return str.toString();
    }
}
